package com.yugu.park.manage.factory;

import cn.hutool.core.lang.UUID;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentParser;
import com.yugu.park.system.entity.SysLoginLogEntity;
import com.yugu.park.system.service.SysLoginLogService;
import com.yugu.park.system.service.impl.SysLoginLogServiceImpl;
import com.yugu.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yugu.park.security.entity.SysLogininfor;
import com.yugu.park.security.entity.TSysOperLog;
import com.yugu.park.security.service.TSysOperLogService;

import java.util.Date;
import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 * @author lei_w
 *
 */
public class AsyncFactory {
	
	private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

    /**
     * 记录登录信息
     * 
     * @param username 用户名
     * @param status 状态
     * @param message 消息
     * @param args 列表
     * @return 任务task
     */
    public static TimerTask recordLogininfor(final String username, final String status, final String message,
            final Object... args)
    {
        final UserAgent userAgent = UserAgentParser.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        return new TimerTask()
        {
            @Override
            public void run()
            {
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                sys_user_logger.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOs().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLoginLogEntity logininfor = new SysLoginLogEntity();
                logininfor.setId(UUID.randomUUID().toString());
                logininfor.setUserName(username);
                logininfor.setLoginTime(new Date());
                logininfor.setIpaddr(ip);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
                {
                	logininfor.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                	logininfor.setStatus(Constants.FAIL);
                }
                // 插入数据
                SpringUtils.getBeanContext(SysLoginLogService.class).insertLoginLog(logininfor);
            }
        };
    }
    /**
     * 操作日志记录
     * 
     * @param operLog 操作日志信息
     * @return 任务task
     */
    public static TimerTask recordOper(final TSysOperLog operLog){
        return new TimerTask()
        {
            @Override
            public void run()
            {
                SpringUtil.getBean(TSysOperLogService.class).addOrUpdate(operLog);
            }
        };
    }
    
}
